<!DOCTYPE HTML>
<!--
	Dimension by HTML5 UP
	html5up.net | @ajlkn
	Free for personal and commercial use under the CCA 3.0 license (html5up.net/license)
-->
<html>
 <head>
  <title>
   Dimension by HTML5 UP
  </title>
  <!-- <meta charset="utf-8" /> -->
  <!-- <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" /> -->
  <meta charset="utf-8"/>
  <meta content="width=device-width,initial-scale=1.0" name="viewport"/>
  <link href="../../assets/css/article.css" rel="stylesheet"/>
  <link href="https://cdn.bootcss.com/highlight.js/9.15.8/styles/github.min.css" rel="stylesheet"/>
  <noscript>
   <link href="../../assets/css/noscript.css" rel="stylesheet"/>
  </noscript>
 </head>
 <body>
  <div id="app">
  </div>
  <!-- built files will be auto injected -->
 </body>
 <body class="is-preload">
  <!-- Wrapper -->
  <div id="wrapper">
   <!-- Main -->
   <div id="main">
    <article id="article">
     <h1 id="shardingsphere-raw-jdbc">
      ShardingSphere Raw JDBC 主从示例
     </h1>
     <hr/>
     <h2 id="_1">
      总览
     </h2>
     <p>
      这个示例不结合ORM之类的，直接执行SQL语句。通过写两个简单的文件就可以了，大致的步骤如下：
     </p>
     <ul>
      <li>
       1.配置Maven依赖
      </li>
      <li>
       2.配置文件写入相关配置
      </li>
      <li>
       3.获取DataSource类编写
      </li>
      <li>
       4.测试
      </li>
     </ul>
     <h3 id="maven">
      配置Maven依赖
     </h3>
     <p>
      其主要依赖如下，还有一些spring、mysql的依赖，总的Maven文件链接在这：
      <a href="">
       pom.xml
      </a>
      ，自定查找添加吧
     </p>
     <div class="codehilite">
      <pre><span></span><code><span class="o">&lt;</span><span class="n">dependency</span><span class="o">&gt;</span>
    <span class="o">&lt;</span><span class="n">groupId</span><span class="o">&gt;</span><span class="n">org</span><span class="p">.</span><span class="na">apache</span><span class="p">.</span><span class="na">shardingsphere</span><span class="o">&lt;/</span><span class="n">groupId</span><span class="o">&gt;</span>
    <span class="o">&lt;</span><span class="n">artifactId</span><span class="o">&gt;</span><span class="n">shardingsphere</span><span class="o">-</span><span class="n">jdbc</span><span class="o">-</span><span class="n">core</span><span class="o">&lt;/</span><span class="n">artifactId</span><span class="o">&gt;</span>
    <span class="o">&lt;</span><span class="n">version</span><span class="o">&gt;</span><span class="mf">5.0.0</span><span class="o">-</span><span class="n">alpha</span><span class="o">&lt;/</span><span class="n">version</span><span class="o">&gt;</span>
<span class="o">&lt;/</span><span class="n">dependency</span><span class="o">&gt;</span>
</code></pre>
     </div>
     <h3 id="applicationproperties">
      application.properties文件写入
     </h3>
     <p>
      写入主从库的相关配置
     </p>
     <div class="codehilite">
      <pre><span></span><code><span class="err">#</span> <span class="n">读写分离</span> <span class="o">-</span> <span class="n">数据库框架版本</span> <span class="mf">2.0</span> <span class="n">ShardingSphere</span><span class="o">-</span><span class="n">jdbc</span> <span class="mf">5.0.0</span><span class="o">-</span><span class="n">alpha</span>
<span class="n">sharding</span><span class="p">.</span><span class="na">jdbc</span><span class="p">.</span><span class="na">datasource</span><span class="p">.</span><span class="na">names</span><span class="o">=</span><span class="n">master</span><span class="p">,</span><span class="n">slave0</span><span class="p">,</span><span class="n">slave1</span>

<span class="n">sharding</span><span class="p">.</span><span class="na">jdbc</span><span class="p">.</span><span class="na">datasource</span><span class="p">.</span><span class="na">ds</span><span class="o">-</span><span class="n">master</span><span class="p">.</span><span class="na">driver</span><span class="o">-</span><span class="n">class</span><span class="o">-</span><span class="n">name</span><span class="o">=</span><span class="n">com</span><span class="p">.</span><span class="na">mysql</span><span class="p">.</span><span class="na">jdbc</span><span class="p">.</span><span class="na">Driver</span>
<span class="n">sharding</span><span class="p">.</span><span class="na">jdbc</span><span class="p">.</span><span class="na">datasource</span><span class="p">.</span><span class="na">ds</span><span class="o">-</span><span class="n">master</span><span class="p">.</span><span class="na">url</span><span class="o">=</span><span class="n">jdbc</span><span class="p">:</span><span class="n">mysql</span><span class="p">:</span><span class="c1">//localhost:3306/test?serverTimezone=UTC&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false&amp;allowPublicKeyRetrieval=true</span>
<span class="n">sharding</span><span class="p">.</span><span class="na">jdbc</span><span class="p">.</span><span class="na">datasource</span><span class="p">.</span><span class="na">ds</span><span class="o">-</span><span class="n">master</span><span class="p">.</span><span class="na">username</span><span class="o">=</span><span class="n">root</span>
<span class="n">sharding</span><span class="p">.</span><span class="na">jdbc</span><span class="p">.</span><span class="na">datasource</span><span class="p">.</span><span class="na">ds</span><span class="o">-</span><span class="n">master</span><span class="p">.</span><span class="na">password</span><span class="o">=</span><span class="n">root</span>

<span class="n">sharding</span><span class="p">.</span><span class="na">jdbc</span><span class="p">.</span><span class="na">datasource</span><span class="p">.</span><span class="na">ds</span><span class="o">-</span><span class="n">slave0</span><span class="p">.</span><span class="na">driver</span><span class="o">-</span><span class="n">class</span><span class="o">-</span><span class="n">name</span><span class="o">=</span><span class="n">com</span><span class="p">.</span><span class="na">mysql</span><span class="p">.</span><span class="na">jdbc</span><span class="p">.</span><span class="na">Driver</span>
<span class="n">sharding</span><span class="p">.</span><span class="na">jdbc</span><span class="p">.</span><span class="na">datasource</span><span class="p">.</span><span class="na">ds</span><span class="o">-</span><span class="n">slave0</span><span class="p">.</span><span class="na">url</span><span class="o">=</span><span class="n">jdbc</span><span class="p">:</span><span class="n">mysql</span><span class="p">:</span><span class="c1">//localhost:3309/test?serverTimezone=UTC&amp;useUnicode=true\</span>
  <span class="o">&amp;</span><span class="n">characterEncoding</span><span class="o">=</span><span class="n">utf</span><span class="o">-</span><span class="mi">8</span><span class="err">\</span>
  <span class="o">&amp;</span><span class="n">useSSL</span><span class="o">=</span><span class="kc">false</span><span class="o">&amp;</span><span class="n">allowPublicKeyRetrieval</span><span class="o">=</span><span class="kc">true</span>
<span class="n">sharding</span><span class="p">.</span><span class="na">jdbc</span><span class="p">.</span><span class="na">datasource</span><span class="p">.</span><span class="na">ds</span><span class="o">-</span><span class="n">slave0</span><span class="p">.</span><span class="na">username</span><span class="o">=</span><span class="n">root</span>
<span class="n">sharding</span><span class="p">.</span><span class="na">jdbc</span><span class="p">.</span><span class="na">datasource</span><span class="p">.</span><span class="na">ds</span><span class="o">-</span><span class="n">slave0</span><span class="p">.</span><span class="na">password</span><span class="o">=</span><span class="n">root</span>

<span class="n">sharding</span><span class="p">.</span><span class="na">jdbc</span><span class="p">.</span><span class="na">datasource</span><span class="p">.</span><span class="na">ds</span><span class="o">-</span><span class="n">slave1</span><span class="p">.</span><span class="na">driver</span><span class="o">-</span><span class="n">class</span><span class="o">-</span><span class="n">name</span><span class="o">=</span><span class="n">com</span><span class="p">.</span><span class="na">mysql</span><span class="p">.</span><span class="na">jdbc</span><span class="p">.</span><span class="na">Driver</span>
<span class="n">sharding</span><span class="p">.</span><span class="na">jdbc</span><span class="p">.</span><span class="na">datasource</span><span class="p">.</span><span class="na">ds</span><span class="o">-</span><span class="n">slave1</span><span class="p">.</span><span class="na">url</span><span class="o">=</span><span class="n">jdbc</span><span class="p">:</span><span class="n">mysql</span><span class="p">:</span><span class="c1">//localhost:3310/test?serverTimezone=UTC&amp;useUnicode=true&amp;characterEncoding=utf-8\</span>
  <span class="o">&amp;</span><span class="n">useSSL</span><span class="o">=</span><span class="kc">false</span><span class="o">&amp;</span><span class="n">allowPublicKeyRetrieval</span><span class="o">=</span><span class="kc">true</span>
<span class="n">sharding</span><span class="p">.</span><span class="na">jdbc</span><span class="p">.</span><span class="na">datasource</span><span class="p">.</span><span class="na">ds</span><span class="o">-</span><span class="n">slave1</span><span class="p">.</span><span class="na">username</span><span class="o">=</span><span class="n">root</span>
<span class="n">sharding</span><span class="p">.</span><span class="na">jdbc</span><span class="p">.</span><span class="na">datasource</span><span class="p">.</span><span class="na">ds</span><span class="o">-</span><span class="n">slave1</span><span class="p">.</span><span class="na">password</span><span class="o">=</span><span class="n">root</span>
</code></pre>
     </div>
     <h3 id="shardingmasterslavedatasource">
      ShardingMasterSlaveDataSource编写
     </h3>
     <p>
      这里默认使用HikariDataSource,思路主要是生成各个主从库的链接，放到Sharding中，代码中也有说明，总体比较简单：
     </p>
     <div class="codehilite">
      <pre><span></span><code><span class="kn">package</span> <span class="nn">com.example.demo.shardingsphere.raw.jdbc</span><span class="p">;</span>

<span class="kn">import</span> <span class="nn">com.zaxxer.hikari.HikariDataSource</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">io.shardingsphere.api.config.MasterSlaveRuleConfiguration</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">io.shardingsphere.shardingjdbc.api.MasterSlaveDataSourceFactory</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">lombok.extern.slf4j.Slf4j</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.beans.factory.annotation.Autowired</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.core.env.Environment</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.stereotype.Component</span><span class="p">;</span>

<span class="kn">import</span> <span class="nn">javax.sql.DataSource</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">java.sql.SQLException</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">java.util.*</span><span class="p">;</span>

<span class="cm">/**</span>
<span class="cm"> * @author lw</span>
<span class="cm"> */</span>
<span class="nd">@Slf4j</span>
<span class="nd">@Component</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">ShardingMasterSlaveDataSource</span> <span class="p">{</span>

    <span class="kd">private</span> <span class="kd">final</span> <span class="n">String</span> <span class="n">DRIVER</span> <span class="o">=</span> <span class="s">".driver-class-name"</span><span class="p">;</span>
    <span class="kd">private</span> <span class="kd">final</span> <span class="n">String</span> <span class="n">URL</span> <span class="o">=</span> <span class="s">".url"</span><span class="p">;</span>
    <span class="kd">private</span> <span class="kd">final</span> <span class="n">String</span> <span class="n">USERNAME</span> <span class="o">=</span> <span class="s">".username"</span><span class="p">;</span>
    <span class="kd">private</span> <span class="kd">final</span> <span class="n">String</span> <span class="n">PASSWORD</span> <span class="o">=</span> <span class="s">".password"</span><span class="p">;</span>
    <span class="kd">private</span> <span class="kd">final</span> <span class="n">String</span> <span class="n">DBS</span> <span class="o">=</span> <span class="s">"sharding.jdbc.datasource.names"</span><span class="p">;</span>

    <span class="nd">@Autowired</span>
    <span class="kd">private</span> <span class="n">Environment</span> <span class="n">environment</span><span class="p">;</span>

    <span class="n">DataSource</span> <span class="nf">createDataSource</span><span class="p">()</span> <span class="kd">throws</span> <span class="n">SQLException</span> <span class="p">{</span>
        <span class="c1">// 获取数据库列表</span>
        <span class="n">String</span><span class="o">[]</span> <span class="n">dbs</span> <span class="o">=</span> <span class="n">Objects</span><span class="p">.</span><span class="na">requireNonNull</span><span class="p">(</span><span class="n">environment</span><span class="p">.</span><span class="na">getProperty</span><span class="p">(</span><span class="n">DBS</span><span class="p">)).</span><span class="na">split</span><span class="p">(</span><span class="s">","</span><span class="p">);</span>
        <span class="n">log</span><span class="p">.</span><span class="na">info</span><span class="p">(</span><span class="s">"DBS::"</span> <span class="o">+</span> <span class="n">Arrays</span><span class="p">.</span><span class="na">toString</span><span class="p">(</span><span class="n">dbs</span><span class="p">));</span>

        <span class="c1">// 设置主从，约定第一个为主，其他为从</span>
        <span class="n">MasterSlaveRuleConfiguration</span> <span class="n">configuration</span> <span class="o">=</span> <span class="k">new</span> <span class="n">MasterSlaveRuleConfiguration</span><span class="p">(</span><span class="n">dbs</span><span class="o">[</span><span class="mi">0</span><span class="o">]</span><span class="p">,</span> <span class="n">dbs</span><span class="o">[</span><span class="mi">0</span><span class="o">]</span><span class="p">,</span>
                <span class="n">Arrays</span><span class="p">.</span><span class="na">asList</span><span class="p">(</span><span class="n">Arrays</span><span class="p">.</span><span class="na">copyOfRange</span><span class="p">(</span><span class="n">dbs</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">dbs</span><span class="p">.</span><span class="na">length</span><span class="p">)));</span>
        <span class="n">log</span><span class="p">.</span><span class="na">info</span><span class="p">(</span><span class="s">"ShardingMasterSlaveDataSource master :: "</span> <span class="o">+</span> <span class="n">configuration</span><span class="p">.</span><span class="na">getMasterDataSourceName</span><span class="p">());</span>
        <span class="n">log</span><span class="p">.</span><span class="na">info</span><span class="p">(</span><span class="s">"ShardingMasterSlaveDataSource slave :: "</span> <span class="o">+</span> <span class="n">configuration</span><span class="p">.</span><span class="na">getSlaveDataSourceNames</span><span class="p">());</span>

        <span class="c1">// 设置打印SQL语句，查看主从配置和切换是否成功</span>
        <span class="n">Properties</span> <span class="n">properties</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Properties</span><span class="p">();</span>
        <span class="n">properties</span><span class="p">.</span><span class="na">setProperty</span><span class="p">(</span><span class="s">"sql.show"</span><span class="p">,</span> <span class="s">"true"</span><span class="p">);</span>

        <span class="k">return</span> <span class="n">MasterSlaveDataSourceFactory</span><span class="p">.</span><span class="na">createDataSource</span><span class="p">(</span><span class="n">createDataSourceMap</span><span class="p">(</span><span class="n">dbs</span><span class="p">),</span> <span class="n">configuration</span><span class="p">,</span> <span class="k">new</span> <span class="n">HashMap</span><span class="o">&lt;&gt;</span><span class="p">(</span><span class="mi">0</span><span class="p">),</span>
                <span class="n">properties</span><span class="p">);</span>
    <span class="p">}</span>

    <span class="cm">/**</span>
<span class="cm">     * 返回DataSource列表</span>
<span class="cm">     */</span>
    <span class="kd">private</span> <span class="n">Map</span><span class="o">&lt;</span><span class="n">String</span><span class="p">,</span> <span class="n">DataSource</span><span class="o">&gt;</span> <span class="nf">createDataSourceMap</span><span class="p">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">dbs</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">Map</span><span class="o">&lt;</span><span class="n">String</span><span class="p">,</span> <span class="n">DataSource</span><span class="o">&gt;</span> <span class="n">result</span> <span class="o">=</span> <span class="k">new</span> <span class="n">HashMap</span><span class="o">&lt;&gt;</span><span class="p">(</span><span class="n">dbs</span><span class="p">.</span><span class="na">length</span><span class="p">);</span>
        <span class="k">for</span> <span class="p">(</span><span class="n">String</span> <span class="n">db</span><span class="p">:</span> <span class="n">dbs</span><span class="p">)</span> <span class="p">{</span>
            <span class="n">log</span><span class="p">.</span><span class="na">info</span><span class="p">(</span><span class="s">"Create data source ::"</span> <span class="o">+</span> <span class="n">db</span><span class="p">);</span>
            <span class="n">result</span><span class="p">.</span><span class="na">put</span><span class="p">(</span><span class="n">db</span><span class="p">,</span> <span class="n">createDataSource</span><span class="p">(</span><span class="s">"sharding.jdbc.datasource.ds-"</span> <span class="o">+</span> <span class="n">db</span><span class="p">));</span>
        <span class="p">}</span>
        <span class="k">return</span> <span class="n">result</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="kd">private</span> <span class="n">DataSource</span> <span class="nf">createDataSource</span><span class="p">(</span><span class="n">String</span> <span class="n">prefix</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">log</span><span class="p">.</span><span class="na">info</span><span class="p">(</span><span class="n">DRIVER</span> <span class="o">+</span> <span class="s">"::"</span> <span class="o">+</span> <span class="n">environment</span><span class="p">.</span><span class="na">getProperty</span><span class="p">(</span><span class="n">prefix</span> <span class="o">+</span> <span class="n">DRIVER</span><span class="p">));</span>
        <span class="n">log</span><span class="p">.</span><span class="na">info</span><span class="p">(</span><span class="n">URL</span> <span class="o">+</span> <span class="s">"::"</span> <span class="o">+</span> <span class="n">environment</span><span class="p">.</span><span class="na">getProperty</span><span class="p">(</span><span class="n">prefix</span> <span class="o">+</span> <span class="n">URL</span><span class="p">));</span>
        <span class="n">log</span><span class="p">.</span><span class="na">info</span><span class="p">(</span><span class="n">USERNAME</span> <span class="o">+</span> <span class="s">"::"</span> <span class="o">+</span> <span class="n">environment</span><span class="p">.</span><span class="na">getProperty</span><span class="p">(</span><span class="n">prefix</span> <span class="o">+</span> <span class="n">USERNAME</span><span class="p">));</span>
        <span class="n">log</span><span class="p">.</span><span class="na">info</span><span class="p">(</span><span class="n">PASSWORD</span> <span class="o">+</span> <span class="s">"::"</span> <span class="o">+</span> <span class="n">environment</span><span class="p">.</span><span class="na">getProperty</span><span class="p">(</span><span class="n">prefix</span> <span class="o">+</span> <span class="n">PASSWORD</span><span class="p">));</span>

        <span class="n">HikariDataSource</span> <span class="n">dataSource</span> <span class="o">=</span> <span class="k">new</span> <span class="n">HikariDataSource</span><span class="p">();</span>
        <span class="n">dataSource</span><span class="p">.</span><span class="na">setDriverClassName</span><span class="p">(</span><span class="n">environment</span><span class="p">.</span><span class="na">getProperty</span><span class="p">(</span><span class="n">prefix</span> <span class="o">+</span> <span class="n">DRIVER</span><span class="p">));</span>
        <span class="n">dataSource</span><span class="p">.</span><span class="na">setJdbcUrl</span><span class="p">(</span><span class="n">environment</span><span class="p">.</span><span class="na">getProperty</span><span class="p">(</span><span class="n">prefix</span> <span class="o">+</span> <span class="n">URL</span><span class="p">));</span>
        <span class="n">dataSource</span><span class="p">.</span><span class="na">setUsername</span><span class="p">(</span><span class="n">environment</span><span class="p">.</span><span class="na">getProperty</span><span class="p">(</span><span class="n">prefix</span> <span class="o">+</span> <span class="n">USERNAME</span><span class="p">));</span>
        <span class="n">dataSource</span><span class="p">.</span><span class="na">setPassword</span><span class="p">(</span><span class="n">environment</span><span class="p">.</span><span class="na">getProperty</span><span class="p">(</span><span class="n">prefix</span> <span class="o">+</span> <span class="n">PASSWORD</span><span class="p">));</span>
        <span class="k">return</span> <span class="n">dataSource</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre>
     </div>
     <h3 id="_2">
      测试
     </h3>
     <p>
      直接生写SQL了，就不结合ORM之类的了，这里是通过日志看出是否生效的，日志大致如下
     </p>
     <div class="codehilite">
      <pre><span></span><code><span class="n">Sharding</span><span class="o">-</span><span class="n">Sphere</span><span class="o">-</span><span class="n">SQL</span>                      <span class="p">:</span> <span class="n">Rule</span> <span class="n">Type</span><span class="p">:</span> <span class="n">master</span><span class="o">-</span><span class="n">slave</span>
<span class="n">Sharding</span><span class="o">-</span><span class="n">Sphere</span><span class="o">-</span><span class="n">SQL</span>                      <span class="p">:</span> <span class="n">SQL</span><span class="p">:</span> <span class="n">select</span> <span class="o">*</span> <span class="n">from</span> <span class="n">stores</span> <span class="n">limit</span> <span class="mi">5</span> <span class="p">:::</span> <span class="n">DataSources</span><span class="p">:</span> <span class="n">slave0</span>
<span class="n">Sharding</span><span class="o">-</span><span class="n">Sphere</span><span class="o">-</span><span class="n">SQL</span>                      <span class="p">:</span> <span class="n">Rule</span> <span class="n">Type</span><span class="p">:</span> <span class="n">master</span><span class="o">-</span><span class="n">slave</span>
<span class="n">Sharding</span><span class="o">-</span><span class="n">Sphere</span><span class="o">-</span><span class="n">SQL</span>                      <span class="p">:</span> <span class="n">SQL</span><span class="p">:</span> <span class="n">select</span> <span class="o">*</span> <span class="n">from</span> <span class="n">stores</span> <span class="n">limit</span> <span class="mi">5</span> <span class="p">:::</span> <span class="n">DataSources</span><span class="p">:</span> <span class="n">slave1</span>
<span class="n">Sharding</span><span class="o">-</span><span class="n">Sphere</span><span class="o">-</span><span class="n">SQL</span>                      <span class="p">:</span> <span class="n">Rule</span> <span class="n">Type</span><span class="p">:</span> <span class="n">master</span><span class="o">-</span><span class="n">slave</span>
<span class="n">Sharding</span><span class="o">-</span><span class="n">Sphere</span><span class="o">-</span><span class="n">SQL</span>                      <span class="p">:</span> <span class="n">SQL</span><span class="p">:</span> <span class="n">insert</span> <span class="n">into</span> <span class="nf">stores</span> <span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">description</span><span class="p">)</span> <span class="n">VALUES</span> <span class="p">(</span><span class="s">"name103"</span><span class="p">,</span> <span class="s">"description103"</span><span class="p">);</span> <span class="p">:::</span> <span class="n">DataSources</span><span class="p">:</span> <span class="n">master</span>
<span class="n">Sharding</span><span class="o">-</span><span class="n">Sphere</span><span class="o">-</span><span class="n">SQL</span>                      <span class="p">:</span> <span class="n">Rule</span> <span class="n">Type</span><span class="p">:</span> <span class="n">master</span><span class="o">-</span><span class="n">slave</span>
<span class="n">Sharding</span><span class="o">-</span><span class="n">Sphere</span><span class="o">-</span><span class="n">SQL</span>                      <span class="p">:</span> <span class="n">SQL</span><span class="p">:</span> <span class="n">select</span> <span class="o">*</span> <span class="n">from</span> <span class="n">stores</span> <span class="n">limit</span> <span class="mi">5</span> <span class="p">:::</span> <span class="n">DataSources</span><span class="p">:</span> <span class="n">master</span>
</code></pre>
     </div>
     <p>
      代码如下：
     </p>
     <div class="codehilite">
      <pre><span></span><code><span class="kn">package</span> <span class="nn">com.example.demo.shardingsphere.raw.jdbc</span><span class="p">;</span>

<span class="kn">import</span> <span class="nn">lombok.extern.slf4j.Slf4j</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">org.junit.jupiter.api.Test</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">org.junit.jupiter.api.extension.ExtendWith</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.beans.factory.annotation.Autowired</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.boot.test.context.SpringBootTest</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.test.context.junit.jupiter.SpringExtension</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.transaction.annotation.Transactional</span><span class="p">;</span>

<span class="kn">import</span> <span class="nn">javax.sql.DataSource</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">java.sql.Connection</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">java.sql.SQLException</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">java.sql.Statement</span><span class="p">;</span>

<span class="nd">@Slf4j</span>
<span class="nd">@SpringBootTest</span>
<span class="nd">@ExtendWith</span><span class="p">(</span><span class="n">SpringExtension</span><span class="p">.</span><span class="na">class</span><span class="p">)</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">ShardingMasterSlaveDataSourceTest</span> <span class="p">{</span>

    <span class="nd">@Autowired</span>
    <span class="n">ShardingMasterSlaveDataSource</span> <span class="n">shardingMasterSlaveDataSource</span><span class="p">;</span>

    <span class="cm">/**</span>
<span class="cm">     * 运行后从日志可以看出主从之间的切换和从库负载均衡</span>
<span class="cm">     */</span>
    <span class="nd">@Test</span> <span class="nd">@Transactional</span>
    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">test</span><span class="p">()</span> <span class="kd">throws</span> <span class="n">SQLException</span> <span class="p">{</span>
        <span class="n">DataSource</span> <span class="n">dataSource</span> <span class="o">=</span> <span class="n">shardingMasterSlaveDataSource</span><span class="p">.</span><span class="na">createDataSource</span><span class="p">();</span>
        <span class="n">log</span><span class="p">.</span><span class="na">info</span><span class="p">(</span><span class="s">"ShardingMasterSlaveDataSource info::"</span> <span class="o">+</span> <span class="n">dataSource</span><span class="p">.</span><span class="na">getConnection</span><span class="p">().</span><span class="na">getMetaData</span><span class="p">().</span><span class="na">getURL</span><span class="p">());</span>

        <span class="n">Connection</span> <span class="n">conn</span> <span class="o">=</span> <span class="n">dataSource</span><span class="p">.</span><span class="na">getConnection</span><span class="p">();</span>
        <span class="n">Statement</span> <span class="n">statement</span> <span class="o">=</span> <span class="n">conn</span><span class="p">.</span><span class="na">createStatement</span><span class="p">();</span>

        <span class="n">String</span> <span class="n">sql</span> <span class="o">=</span> <span class="s">"select * from stores limit 5"</span><span class="p">;</span>
        <span class="n">statement</span><span class="p">.</span><span class="na">execute</span><span class="p">(</span><span class="n">sql</span><span class="p">);</span>
        <span class="n">statement</span><span class="p">.</span><span class="na">execute</span><span class="p">(</span><span class="n">sql</span><span class="p">);</span>

        <span class="n">sql</span> <span class="o">=</span> <span class="s">"insert into stores (name, description) VALUES (\"name103\", \"description103\");"</span><span class="p">;</span>
        <span class="n">statement</span><span class="p">.</span><span class="na">execute</span><span class="p">(</span><span class="n">sql</span><span class="p">);</span>

        <span class="n">sql</span> <span class="o">=</span> <span class="s">"select * from stores limit 5"</span><span class="p">;</span>
        <span class="n">statement</span><span class="p">.</span><span class="na">execute</span><span class="p">(</span><span class="n">sql</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre>
     </div>
    </article>
   </div>
   <!-- Footer -->
   <footer id="footer">
    <p class="copyright">
     © Untitled. Design:
     <a href="https://html5up.net">
      HTML5 UP
     </a>
     .
    </p>
   </footer>
  </div>
  <!-- BG -->
  <div id="bg">
  </div>
  <!-- Scripts -->
  <script src="../assets/js/jquery.min.js">
  </script>
  <script src="../assets/js/browser.min.js">
  </script>
  <script src="../assets/js/breakpoints.min.js">
  </script>
  <script src="../assets/js/util.js">
  </script>
  <script src="../assets/js/main.js">
  </script>
 </body>
</html>
